package com.hongyu.smart.village.ui.activity;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.hongyu.smart.village.R;
import com.hongyu.smart.village.config.Constant;
import com.hongyu.smart.village.entity.TabEntity;
import com.hongyu.smart.village.event.Event;
import com.hongyu.smart.village.ui.adapter.FragmentViewPagerAdapter;
import com.hongyu.smart.village.ui.base.BaseToolbarActivity;
import com.hongyu.smart.village.ui.fragment.BulletinFragment;
import com.hongyu.smart.village.ui.fragment.NoticeFragmernt;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;

public class NoticeActivity extends BaseToolbarActivity {

    @BindView(R.id.viewpager)
    ViewPager viewPager;
    @BindView(R.id.tab_layout)
    CommonTabLayout tabLayout;
    int index=0,pagenum=-1,newPosition=-1;
    ArrayList<Fragment> fragments=new ArrayList<>();
    ArrayList<String> titles=new ArrayList<>();
    ArrayList<CustomTabEntity> tabEntities=new ArrayList<>();
    FragmentViewPagerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public String getToolBarTitle() {
        return "通知公告";
    }

    @Override
    protected void initActivityData() {

    }

    @Override
    protected void initContentView() {
        initViewPager();
    }
    private void initViewPager() {
        BulletinFragment bulletinFragment=new BulletinFragment();
        NoticeFragmernt noticeFragmernt=new NoticeFragmernt();
        fragments.add(noticeFragmernt);
        fragments.add(bulletinFragment);
        adapter=new FragmentViewPagerAdapter(getSupportFragmentManager(),titles,fragments);
        viewPager.setAdapter(adapter);
        tabEntities.add(new TabEntity("通知"));
        tabEntities.add(new TabEntity("公告"));
        titles.add("通知");
        titles.add("公告");

        tabLayout.setTabData(tabEntities);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(pagenum==position){


                }else {
                    pagenum=position;
                    tabLayout.setCurrentTab(position);

                    EventBus.getDefault().post(new Event<>(Constant.FRAGMENT_SELECT_CHANGE_EVENT, titles.get(position)));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {


                tabLayout.setCurrentTab(position);
                viewPager.setCurrentItem(position);

                EventBus.getDefault().post(new Event<>(Constant.FRAGMENT_SELECT_CHANGE_EVENT, titles.get(position)));

            }

            @Override
            public void onTabReselect(int position) {


            }
        });

        tabLayout.setCurrentTab(index);

    }
    @Override
    protected int getContentView() {
        return R.layout.activity_notice;
    }

    @Override
    protected void onEventProcess(Event event) {

    }
}